#include "view_time_1.h"
#include "view_time_1.inc"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "weather_ctrl.h"
#include "view_times.h"
#include "math.h"
static void show_time_1_week(void)
{
    disp_draw_user_image(102, 47, img_time_0_week_TAB[disp_ctrl.com_data.i_temp[5] - 1]);
}
static void show_time_1_date(void)
{
    disp_draw_user_image(92 - 70, 206, img_time_0_date_underline);
    disp_draw_user_image(72 - 70, 206, img_time_0_date_TAB[disp_ctrl.com_data.i_temp[1] / 10]);
    disp_draw_user_image(83 - 70, 206, img_time_0_date_TAB[disp_ctrl.com_data.i_temp[1] % 10]);
    disp_draw_user_image(101 - 70, 206, img_time_0_date_TAB[disp_ctrl.com_data.i_temp[2] / 10]);
    disp_draw_user_image(112 - 70, 206, img_time_0_date_TAB[disp_ctrl.com_data.i_temp[2] % 10]);
}
static void show_time_1_time(void)
{
    uint8_t const img_time_1_time_TAB[] = {
        FILE_IMG_2100_time_0_18_8, /* 0 */
        FILE_IMG_2101_time_1_18_8, /* 1 */
        FILE_IMG_2102_time_2_18_8, /* 2 */
        FILE_IMG_2103_time_3_18_8, /* 3 */
        FILE_IMG_2104_time_4_18_8, /* 4 */
        FILE_IMG_2105_time_5_18_8, /* 5 */
        FILE_IMG_2106_time_6_18_8, /* 6 */
        FILE_IMG_2107_time_7_18_8, /* 7 */
        FILE_IMG_2108_time_8_18_8, /* 8 */
        FILE_IMG_2109_time_9_18_8, /* 9 */
    };
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 8, 8, img_time_1_time_TAB[disp_ctrl.com_data.i_temp[3] / 10]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 50, 8, img_time_1_time_TAB[disp_ctrl.com_data.i_temp[3] % 10]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 8, 104, img_time_1_time_TAB[disp_ctrl.com_data.i_temp[4] / 10]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, 50, 104, img_time_1_time_TAB[disp_ctrl.com_data.i_temp[4] % 10]);
}

static void show_time_1_percent(uint8_t const *base, uint8_t const *full, int percent, int start_y, int x)
{
    if (percent < 0)
    {
        percent = 0;
    }
    else if (percent > 100)
    {
        percent = 100;
    }
    int show_percent = percent;
    disp_draw_user_image_ignore_color(x, 0, RGB565_COLOR_BLACK, base);
    int height = 240 - start_y;
    int y_base = start_y + (100 - show_percent) * height / 100;
    disp_draw_user_image_over_y(x, 0, full, y_base);
}
static void show_time_1_calorie(void)
{
    int percent = disp_ctrl.com_data.i_temp[16] * 100 / system_config_get_target_calorie();
    show_time_1_percent(img_time_1_calorie_percent_base, img_time_1_calorie_percent_full, percent, 0, 144);
    disp_draw_user_image_filter_color(164, 208, img_time_1_calorie);
}
static void show_time_1_steps(void)
{
    int percent = disp_ctrl.com_data.i_temp[11] * 100 / system_config_get_target_step();
    show_time_1_percent(img_time_1_steps_percent_base, img_time_1_steps_percent_full, percent, 0, 98);
    disp_draw_user_image_filter_color(117, 207, img_time_1_steps);
}
static void show_time_1_heart_rate(void)
{
    //!< 40 为 0% , 最大心率为 100 %
    int min_heart_rate = 40;
    int max_heart_rate = 220 - system_config_get_age();
    int percent = (disp_ctrl.com_data.i_temp[17] - min_heart_rate) * 100 / (max_heart_rate - min_heart_rate);

    show_time_1_percent(img_time_1_heart_rate_percent_base, img_time_1_heart_rate_percent_full, percent, 85, 189);
    disp_draw_user_image_filter_color(209, 209, img_time_1_heart_rate);
}
void view_time_1_display(void)
{
    show_time_1_time();
    show_time_1_week();
    show_time_1_date();
    show_time_1_heart_rate();
    show_time_1_calorie();
    show_time_1_steps();
}
